package com.songlanyun.common.utils.custom;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateCustomUtils {

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";


    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 字符串转毫秒时间戳
     *
     * @param date
     * @return
     */
    public static long dateStringToMilliSecond(String date) {

        if ("".equals(date) || "null".equals(date) || null == date) {
            return 0;
        }
        try {
            if (date.contains(":")) {
                return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime());
            } else {
                return (new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime());
            }
        } catch (Exception e) {
            throw new Error("转换异常");
        }
    }

    /**
     * 字符串转秒时间戳
     *
     * @param date
     * @return
     */
    public static int dateStringToSecond(String date) {
        if ("".equals(date) || "null".equals(date) || null == date) {
            return 0;
        }
        try {
            return ((int) (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime() / 1000));
        } catch (Exception e) {
            throw new Error("转换异常");
        }
    }


    /**
     * 把时间戳（毫秒）转成日期
     *
     * @param date
     * @return
     */
    public static String formatToDateString(Long date) {
        if (date == null || date == 0) {
            return StringUtils.EMPTY;
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(date));
    }

    public static long getNow() {
        return System.currentTimeMillis();
    }

    /**
     * 把时间戳（毫秒）转成日期时间
     *
     * @param date
     * @return
     */
    public static String formatToDateTimeString(Long date) {
        if (date == null || date == 0) {
            return StringUtils.EMPTY;
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date));
    }


    /**
     * 把时间戳（毫秒）转成日期时间
     *
     * @param date
     * @return
     */
    public static Date formatToDate(Long date) {
        if (date == null || date == 0) {
            return null;
        }
        return new Date(date);
    }


    public static String format(Long date, String pattern) {
        if (date == null || date == 0) {
            return StringUtils.EMPTY;
        }
        return new SimpleDateFormat(pattern).format(new Date(date));
    }

    public static String format(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static long parse(String date, String pattern) {

        if (date == null || date.isEmpty()) return 0;

        try {
            return new SimpleDateFormat(pattern).parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取本周的第一天
     * data 指定時間
     *
     * @return Date
     **/
    public static Date getWeekStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return stringToDate(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00");
    }


    /**
     * 获取本周的最后一天
     * data 指定時間
     *
     * @return Date
     **/
    public static Date getWeekEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time = cal.getTime();
        return stringToDate(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59");
    }


    /**
     * 获取本月开始日期
     * data 指定時間
     *
     * @return Date
     **/
    public static Date getMonthStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return stringToDate(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00");
    }

    /**
     * 获取本月最后一天
     * data 指定時間
     *
     * @return Date
     **/
    public static Date getMonthEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = cal.getTime();
        return stringToDate(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59");
    }


    /**
     * 获取周
     *
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);


    }


    /**
     * 获取周
     *
     * @param date
     * @return
     */
    public static int getDayOfWeek(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }


    /**
     * 获取几号
     *
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取某个月最后一天
     */
    public static int getMonthLastDay(int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), month, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 秒时间戳转字符串
     *
     * @param date
     * @return
     */
    public static String secondToDateString(int date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date * 1000L));
    }

    /**
     * 秒时间戳转字符串
     *
     * @param date
     * @return
     */
    public static String secondToDateString2(long date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date));
    }

    /**
     * 时间转字符串
     *
     * @param date
     * @return
     */
    public static String stringToDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 字符串转时间
     *
     * @param date
     * @return
     */

    public static Date stringToDateTime(String date) {
        if ("".equals(date) || "null".equals(date) || null == date) {
            return null;
        }
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
        } catch (Exception e) {
            throw new Error("转换异常");
        }
    }

    public static Date stringToDate(String date) {
        if ("".equals(date) || "null".equals(date) || null == date) {
            return null;
        }
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (Exception e) {
            throw new Error("转换异常");
        }
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
        return sformat.format(date);

    }

    /**
     * 获取两个日期中间的日期集合
     *
     * @param start
     * @param end
     * @return
     */
    public static List<Date> getBetweenDates(Date start, Date end) {


        List<Date> result = new ArrayList<Date>();
        result.add(start);

        if (start.equals(end)) {//起止日期相同
            return result;
        }

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        result.add(end);
        return result;
    }

    /**
     * 获取日期集合中在在指定周几的所有日期
     *
     * @return
     */
    public static List<Date> getBetweenDates(List<Date> betweenDates, List<Integer> weekList) {
        List<Date> list = new ArrayList<>();
        for (Date betweenDate : betweenDates) {
            int weekOfDate = getWeekOfDate(betweenDate);
            if (weekList.contains(weekOfDate)) {
                list.add(betweenDate);
            }
        }
        return list;
    }

    /**
     * 获取周 -- 0：周日 1：周一
     *
     * @param date
     * @return
     */
    public static int getWeekOfDate(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;
    }


}
